/* @flow */
import React from 'react';
import { IoAt } from 'react-icons/io';
import { createAction, createActions, handleActions, type ActionType } from 'redux-actions';
import { createSagaActionType, createSagaAction, REQUEST, SUCCESS, FAILURE } from '../sagaUtils';
import produce from 'immer';

/* ACTION TYPES */
const LOAD_CLASSIFY = 'thing/LOAD_CLASSIFY';
const LOAD_THINGS = 'thing/LOAD_THINGS';
const LOAD_CONTEXTS = 'thing/LOAD_CONTEXTS';
const LOAD_INBOX = 'thing/LOAD_INBOX';
const CHANGE_THING = 'thing/CHANGE_THING';
const CHANGE_CLASSIFY_EDITABLE = 'thing/CHANGE_CLASSIFY_EDITABLE';
const CHANGE_CLASSIFY_UNEDITABLE = 'thing/CHANGE_CLASSIFY_UNEDITABLE';
// tree
const CHANGE_TREE_INSERT = 'thing/CHANGE_TREE_INSERT';
const CHANGE_TREE_DROP = 'thing/CHANGE_TREE_DROP';
const SET_DRAGGING_ID = 'thing/SET_DRAGGING_ID';
const SYNC_CLASSIFY = 'thing/SYNC_CLASSIFY';

/* ACTION PAYLOADS */
type UserPayload = {
    id: string,
    email: string
};
type ThingPayload = {
    id: string,
    title: string
};
/* EXPORTING ACTION CREATORS / ACTION CREATORS TYPES */
export interface ThingsActionCreators {
    loadThings_Request(payload: UserPayload): any;
    loadThings_Success(payload: ThingPayload): any;
    loadThings_Request(): any;
}

export const actionCreators: ThingsActionCreators = createActions({
    LOAD_THINGS: {
        REQUEST: (payload: UserPayload) => payload,
        SUCCESS: (payload: ThingPayload) => payload,
        FAILURE: (payload: string) => payload
    },
    CHANGE_CLASSIFY_EDITABLE: (payload: ThingPayload) => payload,
    CHANGE_CLASSIFY_UNEDITABLE: (payload: ThingPayload) => payload
});
/* ACTION FLOW TYPES */
type PlainAction = ActionType<typeof actionCreators.plain>;
/* State Types */
type context = {
    id: string,
    title: string,
    createdTime: number,
    updatedTime: number
};
type thing = {
    id: string,
    title: string,
    time: {
        createdTime: number,
        updatedTime: number,
        completedTime: number,
        estimatedTime: number,
        deferUntilTime: number,
        dueTime: number
    },
    repeat: {
        type: string,
        nextDeferedTime: number,
        nextDueTime: number
    },
    classify: {
        type: string,
        title: string,
        fullTitle: string
    },
    parentThing: {
        id: string,
        title: string,
        fullTitle: string
    },
    context: context,
    status: string,
    flaged: string,
    note: string
};
type ClassifySimple = {
    id: string,
    pid: string,
    level: number,
    type: 'folder' | 'classify',
    classifyType?: 'Parallel' | 'sequential' | 'Single Actions',
    title: string,
    fullTitle: string,
    itemPos: object
};
type ThingSimple = {
    pid: string,
    id: string,
    title: string,
    level: number,
    index: number,
    isOpen: boolean,
    isLeaf: boolean,
    leafIcon: Node,
    labelIcon: Node
};
export type Things = {
    classify: Array<ClassifySimple>,
    context: Array<ContextSimple>,
    things: Array<ThingSimple>,
    inbox: Array<ThingSimple>,
    isChange: boolean,
    isUpload: boolean,
    thing: Thing
};

const initialState: Things = {
    classify: [
        {
            pid: '',
            id: '1',
            title: '1',
            fullTitle: '1',
            index: 1,
            disabled: false,
            level: 0
        },
        { pid: '', id: '2', title: '2', fullTitle: '2', index: 2, disabled: true, level: 0 },
        {
            pid: '',
            id: '3',
            title: '3',
            fullTitle: '2',
            index: 3,
            disabled: false,
            level: 0
        },
        { pid: '', id: '4', title: '4', fullTitle: '2', index: 4, disabled: false, level: 0 }
    ],
    context: [],
    inbox: [],
    things: [],
    isChange: false,
    isUpload: false,
    thing: null
};

export default handleActions(
    {
        [LOAD_THINGS]: (state, action: PlainAction) => state
    },
    initialState
);

export function* RootSaga() {
    console.log('sample Hello Sagas!');
}
